/**
 * File  : PlayThread.cc
 * Time  : 2015-6-24 14:38
 * Author: Yangbo 
 * Ref   : 
 */
#include "WorkThreads.h"
#include "./log/log.h"
#include "./ThreadMutex.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <unistd.h>
using namespace std;

void LogThread::Run() {
	cout<<"log thread"<<endl;
	system("rm ./log/*");
  SettingLogger* setout  = new SettingLogger;
	PlayingLogger* playout = new PlayingLogger;
	NettingLogger* netout  = new NettingLogger;

  setout->LogConfig();
  playout->LogConfig();
  netout->LogConfig();

  if(!setout->OpenFile()) {
    cerr<< "fatal error!" <<endl;
    pthread_exit((void*)2);

  }
  if(!playout->OpenFile()) {
    cerr<< "fatal error!" <<endl;
    pthread_exit((void*)2);

  }
  if(!netout->OpenFile()) {
    cerr<< "fatal error!" <<endl;
    pthread_exit((void*)2);

  }

  for(;;) {

    if(setout->IfRotateLog()) {
			cout<<"going to rotate file "<<endl;
      setout->RotateLog();
    }
    if(playout->IfRotateLog()) {
			cout<<"going to rotate file "<<endl;
      playout->RotateLog();
    }
    if(netout->IfRotateLog()) {
			cout<<"going to rotate file "<<endl;
      netout->RotateLog();
    }

    Type<LogType>* tmp = event_queue_->GetEvent();
		if(tmp!=NULL){
    	setout->SetLevel((tmp->opcode()).level);
    	string str_tmp = tmp->opcode().msg;
    	setout->log(str_tmp);
		}
		
    tmp = ack_queue_->GetEvent();
		if(tmp!=NULL){
    	playout->SetLevel((tmp->opcode()).level);
    	string str_tmp = tmp->opcode().msg;
    	playout->log(str_tmp);
		}

	 tmp = log_queue_->GetEvent();
		if(tmp!=NULL){
			cout<<tmp->opcode().msg<<endl;
    	netout->SetLevel((tmp->opcode()).level);
    	string str_tmp = tmp->opcode().msg;
			cout<<"str_tmp"<<str_tmp<<endl;
    	netout->log(str_tmp);
		}	
	  sleep(2);
  //  delete tmp;

  }
}
